package com.yc.web.servlets.admin;

import com.yc.bean.tb_order;
import com.yc.bean.tb_order_detail;
import com.yc.web.servlets.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author zwei
 * @create 2024-07-01 16:56
 */
@WebServlet("/admin/detail.action")
public class tbOrderDetailServlet extends BaseServlet {
    /**
     * 查找所有订单对应的出餐列表
     *
     * @param req
     * @param resp
     * @throws InvocationTargetException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IOException
     */
    public void findAllDetails(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        tb_order_detail detail = super.parseObjectFromRequest(req, tb_order_detail.class);
        int page = 1;
        if (req.getParameter("page") != null) {
            page = Integer.parseInt(req.getParameter("page"));
        }
        int limit = 10;
        if (req.getParameter("limit") != null) {
            limit = Integer.parseInt(req.getParameter("limit"));
        }
        String sql1 = "SELECT od.*,t.table_name,f.food_name,DATE_FORMAT(o.order_date, '%Y-%m-%d %H:%i:%s') AS order_date\n" +
                "FROM tb_order_detail od\n" +
                "         JOIN tb_food f ON od.foodid = f.id\n" +
                "         JOIN tb_order o ON od.orderid = o.id\n" +
                "         JOIN tb_dinner_table t ON o.table_id=t.id\n" +
                "  where 1=1   ";
        String sql2 = "select count(*) as total from(SELECT od.*,t.table_name,f.food_name,DATE_FORMAT(o.order_date, '%Y-%m-%d %H:%i:%s') AS order_date\n" +
                "                FROM tb_order_detail od\n" +
                "                       JOIN tb_food f ON od.foodid = f.id\n" +
                "                      JOIN tb_order o ON od.orderid = o.id\n" +
                "                   JOIN tb_dinner_table t ON o.table_id=t.id \n"+
                "                    where 1=1   ";
        int skip = (page - 1) * limit;
        List<Object> params = new ArrayList<>();
        if (detail.getOrderid() != null) {
            sql1 += " and orderid=? ";
            sql2 += " and orderid=? ";
            params.add(detail.getOrderid());
        }
        if (req.getParameter("table_name") != null && !req.getParameter("table_name").equals("")) {
            sql1 += " and t.table_name=? ";
            sql2 += " and t.table_name=? ";
            params.add(req.getParameter("table_name"));
        }
        sql2=sql2+"  )AS result";
        List<Map<String, Object>> list = dbHelper.select(sql1 + " ORDER BY CASE WHEN od.disabled = 0 THEN 0 ELSE 1 END, o.pay_date DESC  limit " + limit + " offset " + skip, params.toArray());

        System.out.println(sql1);
        Map<String, Object> map = new HashMap<String, Object>();
        List<Map<String, Object>> result = dbHelper.select(sql2, params.toArray());
        int total = Integer.parseInt(result.get(0).get("total").toString());
        map.put("code", 0);
        map.put("msg", "查询成功");
        map.put("count", total);
        map.put("data", list);
        super.writeObjectToJson(map, resp);
    }

    /**
     * 修改出餐的状态
     *
     * @param req
     * @param resp
     * @throws IOException
     */
    public void deldetail(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String id = req.getParameter("idsStr");
        String[] parts = id.split(",");
        String sql = "update tb_order_detail set disabled=? where id=?";
        for (String str : parts) {
            dbHelper.doUpdate(sql, 1, str);
        }
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "修改成功");
        super.writeObjectToJson(map, resp);
    }

    /**
     * 对出餐列表进行修改
     *
     * @param req
     * @param resp
     * @throws InvocationTargetException
     * @throws InstantiationException
     * @throws IllegalAccessException
     * @throws IOException
     */
    public void update(HttpServletRequest req, HttpServletResponse resp) throws InvocationTargetException, InstantiationException, IllegalAccessException, IOException {
        tb_order_detail detail = super.parseObjectFromRequest(req, tb_order_detail.class);
        String sql = "update tb_order_detail set orderid=?,food_id=?,buynum=?,discount=?,disabled=? where id=?";
        dbHelper.doUpdate(sql, detail.getOrderid(), detail.getFood_id(), detail.getBuynum(), detail.getDisabled(), detail.getDisabled(), detail.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("msg", "修改成功");
        super.writeObjectToJson(map, resp);
    }
}
